function convert(input) { alert('Error. Contact us if this keeps happening.'); document.getElementById('modal-contact').classList.add('show'); } window.ctMaxUploadFiles = 1; var _vtState = { blob: null, fileName: 'video', start: 0, end: 0, duration: 0, ext: 'mp4' }; var _vtFfmpeg = null; function _vtFormatTime(t) { if (!isFinite(t) || t < 0) t = 0; var m = Math.floor(t / 60); var s = (t - m * 60); var ss = s.toFixed(2); if (s < 10) ss = '0' + ss; return m + ':' + ss; } function _vtEnsureFfmpeg() { if (_vtFfmpeg) return Promise.resolve(_vtFfmpeg); return loadScriptPromise('https://unpkg.com/\u0040ffmpeg/ffmpeg\u00400.11.6/dist/ffmpeg.min.js').then(function() { var ns = window.FFmpeg || {}; if (!ns.createFFmpeg) throw new Error('FFmpeg library failed to load'); var ffmpeg = ns.createFFmpeg({ log: true, corePath: 'https://unpkg.com/\u0040ffmpeg/core\u00400.11.0/dist/ffmpeg-core.js' }); _vtFfmpeg = { ffmpeg: ffmpeg, fetchFile: ns.fetchFile }; return ffmpeg.load().then(function(){ return _vtFfmpeg; }); }); } function _vtBindHandles() { var track = document.getElementById('vt-track'); var h1 = document.getElementById('vt-h1'); var h2 = document.getElementById('vt-h2'); if (!track || track._vtBound) return; track._vtBound = true; function startDrag(handle, e) { e.preventDefault(); var rect = track.getBoundingClientRect(); function move(ev) { var p = (ev.touches ? ev.touches[0].clientX : ev.clientX) - rect.left; var pct = Math.max(0, Math.min(100, (p / rect.width) * 100)); handle.style.left = pct + '%'; _vtUpdateFromHandles(); } function up() { document.removeEventListener('pointermove', move); document.removeEventListener('pointerup', up); } document.addEventListener('pointermove', move); document.addEventListener('pointerup', up); } h1.addEventListener('pointerdown', function(e){ startDrag(h1, e); }); h2.addEventListener('pointerdown', function(e){ startDrag(h2, e); }); } function _vtUpdateFromHandles() { var h1pct = parseFloat(document.getElementById('vt-h1').style.left) || 0; var h2pct = parseFloat(document.getElementById('vt-h2').style.left) || 100; var lo = Math.min(h1pct, h2pct), hi = Math.max(h1pct, h2pct); var dur = _vtState.duration; _vtState.start = (lo / 100) * dur; _vtState.end = (hi / 100) * dur; var fill = document.getElementById('vt-fill'); if (fill) { fill.style.left = lo + '%'; fill.style.width = (hi - lo) + '%'; } document.getElementById('vt-start').textContent = _vtFormatTime(_vtState.start); document.getElementById('vt-end').textContent = _vtFormatTime(_vtState.end); document.getElementById('vt-duration').textContent = '/ ' + _vtFormatTime(_vtState.end - _vtState.start); var video = document.getElementById('vt-video'); if (video && Math.abs(video.currentTime - _vtState.start) > 0.5) { try { video.currentTime = _vtState.start; } catch (e) {} } } function processFile(blob, fileName) { _vtState.blob = blob; _vtState.fileName = fileName || 'video.mp4'; _vtState.ext = ((fileName || '').toLowerCase().split('.').pop() || 'mp4'); $('#file-drop-zone').addClass('collapsedDropZone'); (function(){ var $b=$('#file-drop-zone .upload-file-button'); var l=$b.data('change-label'); if(l) $b.text(l); })(); var ws = document.getElementById('vt-workspace'); if (ws) ws.style.display = 'block'; var video = document.getElementById('vt-video'); if (video) { video.src = URL.createObjectURL(blob); video.onloadedmetadata = function() { var d = video.duration; _vtState.duration = (isFinite(d) && d > 0) ? d : 0; document.getElementById('vt-h1').style.left = '0%'; document.getElementById('vt-h2').style.left = '100%'; _vtUpdateFromHandles(); }; } _vtBindHandles(); } function _vtApply() { if (window.console && console.log) console.log('[trim-video] apply clicked. blob=', !!_vtState.blob, 'start=', _vtState.start, 'end=', _vtState.end, 'duration=', _vtState.duration); if (!_vtState.blob) { alert('Drop a video first.'); return; } var start = _vtState.start, end = _vtState.end; if (end - start < 0.1) { alert('Pick a longer range to trim.'); return; } var mode = (document.getElementById('vt-precise') || {}).value || 'copy'; var status = document.getElementById('vt-status'); var btn = document.getElementById('vt-apply'); if (status) { status.textContent = 'Loading FFmpeg (first run downloads ~30 MB)...'; status.style.color = ''; } if (btn) btn.disabled = true; var ext = _vtState.ext; var outExt = ext === 'webm' ? 'webm' : 'mp4'; _vtEnsureFfmpeg().then(function(ctx) { if (status) status.textContent = 'Trimming...'; return ctx.fetchFile(_vtState.blob).then(function(buf) { var inName = 'in.' + ext; try { ctx.ffmpeg.FS('unlink', inName); } catch (e) {} try { ctx.ffmpeg.FS('unlink', 'out.' + outExt); } catch (e) {} ctx.ffmpeg.FS('writeFile', inName, buf); var dur = (end - start).toFixed(3); var args; if (mode === 'copy') { args = ['-i', inName, '-ss', start.toFixed(3), '-t', dur, '-c', 'copy', '-avoid_negative_ts', 'make_zero', '-movflags', 'faststart', 'out.' + outExt]; } else { args = ['-i', inName, '-ss', start.toFixed(3), '-t', dur, '-c:v', 'libx264', '-preset', 'veryfast', '-pix_fmt', 'yuv420p', '-c:a', 'aac', '-b:a', '160k', '-movflags', 'faststart', 'out.' + outExt]; } return ctx.ffmpeg.run.apply(ctx.ffmpeg, args).then(function() { var data; try { data = ctx.ffmpeg.FS('readFile', 'out.' + outExt); } catch (e) { data = null; } if (!data || !data.length) { throw new Error('FFmpeg produced no output. Try the Precise (frame-accurate) mode.'); } var mime = outExt === 'webm' ? 'video/webm' : 'video/mp4'; var outBlob = new Blob([data.buffer], { type: mime }); var base = (_vtState.fileName || 'video').replace(/\.[^.]+$/, ''); add_file_output(URL.createObjectURL(outBlob), base + '-trimmed.' + outExt); try { ctx.ffmpeg.FS('unlink', inName); } catch (e) {} try { ctx.ffmpeg.FS('unlink', 'out.' + outExt); } catch (e) {} }); }); }).then(function() { if (status) status.textContent = 'Done.'; if (btn) btn.disabled = false; }).catch(function(err) { var msg = (err && err.message) ? err.message : String(err); if (window.console && console.error) console.error('[trim-video] failed:', err); if (status) { status.textContent = 'Error: ' + msg; status.style.color = '#b91c1c'; } if (btn) btn.disabled = false; }); } function _vtInit() { var apply = document.getElementById('vt-apply'); if (apply) apply.addEventListener('click', _vtApply); } document.addEventListener('DOMContentLoaded', _vtInit); if (document.readyState !== 'loading') _vtInit(); var _loadedScripts = {}; function loadScriptPromise(url) { if (_loadedScripts[url]) return _loadedScripts[url]; _loadedScripts[url] = new Promise(function (resolve, reject) { var s = document.createElement('script'); s.src = url; s.onload = resolve; s.onerror = reject; document.head.appendChild(s); }); return _loadedScripts[url]; } function replaceAll(find, replace, str) { return str.replace(new RegExp(find, 'g'), replace); } function beautify(str) { var result = ''; var length = str.length; var i = 0; var braceCountLeft = 0; var braceCountRight = 0; var withinQuotes = false; while (i < length) { var c = str[i]; if (c == '"' && (i == 0 || c[i - 1] != '\\')) { // non-escaped quotes withinQuotes = !withinQuotes; } if (!withinQuotes && (c == '}' || c == '{' || c == ',')) { console.log('Start####' + result); // look back and remove carriage returns and whitespace that are already there var resultIndex = result.length - 1; while (resultIndex >= 0 && (result[resultIndex] == ' ' || result[resultIndex] == '\r' || result[resultIndex] == '\n' || result[resultIndex] == '\t')) { resultIndex = resultIndex - 1; result = result.substr(0, resultIndex + 1); console.log('char ' + result[resultIndex] + '-----' + result + 'zzz ' + result.length + ' ' + resultIndex); } if (c == '{') { braceCountLeft++; result += c + '\r' + GetTabs(braceCountLeft - braceCountRight); } else if (c == '}') { braceCountRight++; // precede with carriage return result += '\r' + GetTabs(braceCountLeft - braceCountRight) + c; } else if (c == ',') { result += c + '\r' + GetTabs(braceCountLeft - braceCountRight); } var nextChar = ''; // advance through whitespace and remove carriage returns that are already there while (i < length && (str[i + 1] == ' ' || str[i + 1] == '\r' || str[i + 1] == '\n' || str[i + 1] == '\t')) { i++; } } else { result += str[i]; } i++; } return result; } function GetTabs(count) { var result = ''; for (var i = 0; i < count; i++) { result += ' '; } return result; }